<template>
    <div class="block">
        <el-row class="block-item">
            <exSelect label="类型" v-model="type" :dictList="typeList" elstyle="width:120px" size="mini"></exSelect>
            <exInput label="数量" v-model="psize" width="150" size="mini"></exInput>
            <el-button type="primary" @click="query" size="mini" style="height:28px;margin-top:4px">查询</el-button>
        </el-row>
        <exPubTable ref="newTable" :cfg.sync="newcfg" :exData="newList" @getDataList="queryNewList">
            <exColumn label="名称" width="100">
                <template slot-scope="s">
                    <klinePic :code="s.row.f12" :name="s.row.f14"></klinePic>
                </template>
            </exColumn>
        </exPubTable>
    </div>
</template>



<script>
import Vue from "vue";

import klinePic from "../trade/comp/klinePic"

export default {
    components:{
        klinePic,
    },
    data() {
        var that = this;
        return {
            isLoad: false,
            type : "cy",
            typeList:[{"name":"创业",value:"cy"},{"name":"深证",value:"sz"},{"name":"上证",value:"sh"}],
            psize : 50,
            newList:[],
            newcfg:{
                usePage:false,
                showIndex: true,
                btns:[
                    {name:"比对",handle:({row})=>{
                        this.addComp(row);
                    }},
                    // {name:"添加记录",handle:({row})=>{
                    //     this.addRecord(row);
                    // }}
                ],
                columns2: [
                    {prop:"f12",label:"代码",width:80,sortable:true},
                    // {prop:"f14",label:"名称",width:100,sortable:true},
                    {prop:"f2",label:"最新",width:80,sortable:true,align:"right",getColor(s){return that.getRateColor(s.row.f3)}},
                    {prop:"f3",label:"涨跌幅",width:80,sortable:true,align:"right",getColor(s){return that.getRateColor(s.row.f3)},endStr:"%"},
                    {prop:"f4",label:"涨跌额",width:80,sortable:true,align:"right",getColor(s){return that.getRateColor(s.row.f3)}},
                    {prop:"f18",label:"昨收",width:80,sortable:true,align:"right"},
                    {prop:"f17",label:"今开",width:80,sortable:true,align:"right",getColor(s){return that.getColor(s.row.f17,s.row.f18)}},
                    {prop:"f15",label:"最高",width:80,sortable:true,align:"right",getColor(s){return that.getColor(s.row.f15,s.row.f18)}},
                    {prop:"f16",label:"最低",width:80,sortable:true,align:"right",getColor(s){return that.getColor(s.row.f16,s.row.f18)}},
                    {prop:"f7",label:"振幅",width:80,sortable:true,align:"right"},
                    {prop:"f8",label:"换手",width:80,sortable:true,align:"right",endStr:"%"},
                    {prop:"f9",label:"市盈率",width:80,sortable:true,align:"right"},
                    {prop:"totalDay",label:"天数",width:80,sortable:true,align:"right"},
                    {prop:"step1",label:"step1",width:80,sortable:true,align:"right",getColor(s){return that.getRateColor(s.row.step1)},tips:"最近一个波峰/波谷的左侧天数"},
                    {prop:"step1rise",label:"rise1",width:80,sortable:true,align:"right",endStr:"%",getColor(s){return that.getRateColor(s.row.step1rise)},tips:"最近一个波峰/波谷的左侧幅度"},
                    {prop:"step2",label:"step2",width:80,sortable:true,align:"right",getColor(s){return that.getRateColor(s.row.step2)},tips:"最近一个波峰/波谷的右侧天数"},
                    {prop:"step2rise",label:"rise2",width:80,sortable:true,align:"right",endStr:"%",getColor(s){return that.getRateColor(s.row.step2rise)},tips:"最近一个波峰/波谷的右侧幅度"},

                ],
            }
        }
    },
    computed:{
        qryType (){
            var fsval = "";
            if(this.type=="sz"){
                fsval = "m:0+t:6,m:0+t:13";
            }else if(this.type=="sh"){
                fsval = "m:1+t:2,m:1+t:23";
            }else{
                fsval = "m:0+t:80";
            }
            return fsval
        }
    },
    provide () {
      return {
      }
    },

    mounted() {

    },

    methods: {
        addComp(item){
            window.$vue.addCompItem({code:item.f12,name:item.f14});
        },
        query(){
            if(this.$refs.newTable) this.$refs.newTable.reQuery();
        },
        addRecord(row){
            if(!row.f12){
                this.$message.error("代码格式错误");
                return;
            }
            var param ={
                code: row.f12,
                desc: "新股连跌转涨",
            }
            
            this.$axios.post(`${this.$config.sapi}/api/stock/add`,param)
            .then((res) => {
                if (res.data.code == 200) {
                    this.$message.success("添加成功");
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
        queryNewList(){
            this.$axios.get(`http://10.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=${this.psize}&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f12&fs=${this.qryType}&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1604283830757`)
            .then((res) => {
                if (res.data.data) {
                    this.newList = res.data.data.diff;
                    this.newList.map((item)=>{
                        this.getKLineInfo(item);
                    });
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
        
        getKLineInfo(item){
            var code = item.f12;
            // if(code!="300898") return;
            var first = code.substring(0,1);
            if(code=="000001"){
                code= "1."+code;
            }else if(first=="0" || first=="3" || first=="1"){
                code = "0."+code;
            }else {
                code = "1."+code;
            }
            this.$axios.get(`http://push2his.eastmoney.com/api/qt/stock/kline/get?secid=${code}&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&ut=7eea3edcaed734bea9cbfc24409ed989&klt=101&fqt=1&beg=0&end=20500000`)
            .then((res) => {
                if (res.data.data) {
                    this.dataInfo=[];
                    var data = res.data.data;
                    this.name = data.name;
                    var len = data.klines.length;
                    item.totalDay = len;
                    if(len>20){
                        data.klines = data.klines.splice(len-20,20)
                    }
                    // data.klines.map((item,idx)=>{
                    //     if(idx>100) return;
                    //     var arr = item.split(",");
                    //     var row = [];
                    //     row.push(arr[0]);//日期
                    //     row.push(arr[1]*1);//开
                    //     row.push(arr[2]*1);//收
                    //     row.push(arr[3]*1);//高
                    //     row.push(arr[4]*1);//低
                    //     row.push(arr[6]*1);//金额
                    //     row.push(arr[7]*1);//振幅
                    //     row.push(arr[8]*1);//涨跌幅
                    //     row.push(arr[9]*1);//涨跌额
                    //     row.push(arr[10]*1);//换手

                    //     this.dataInfo.push(row);
                    // })
                    item.step1= 0;
                    item.step2= 0;
                    item.step1rise = 0;
                    item.step2rise = 0;
                    var list = data.klines;
                    var len = list.length;
                    var preWave = null;
                    var preArr = null;
                    for(var i=len-1;i>=0;i--){
                        var row = list[i]; 
                        var arr = row.split(",");
                        preArr = arr;
                        var isUp = arr[8]*1>0?true:false;
                        if(i==len-1){
                            preWave = isUp;
                            if(isUp){
                                item.step2++;
                                item.step2rise = arr[3]*1
                            }else{
                                item.step2--;
                                item.step2rise = arr[4]*1
                            }
                            continue;
                        }
                        if(preWave && isUp){
                            if(item.step1==0){
                                item.step2++;
                            }else{
                                item.step1++;
                            }
                        }else if(preWave && !isUp){
                            if(item.step1!=0 && item.step2!=0){
                                item.step1rise = this.getRise(item.step1rise,arr[4]*1)
                                break;
                            }
                            var tmpprice = arr[4]*1;
                            item.step2rise = this.getRise(item.step2rise,tmpprice)
                            item.step1rise = tmpprice
                            item.step1--;
                        }else if(!preWave && isUp){
                            if(item.step1!=0 && item.step2!=0){
                                item.step1rise = this.getRise(item.step1rise,preArr[3]*1)
                                break;
                            }
                            var tmpprice = arr[3]*1;
                            item.step2rise = this.getRise(item.step2rise,tmpprice)
                            item.step1rise = tmpprice
                            item.step1++;
                        }else if(!preWave && !isUp){
                            if(item.step1==0){
                                item.step2--;
                            }else{
                                item.step1--;
                            }
                        }
                        preWave = isUp;
                    }
                    // this.$message.info(`step1:${item.step1}->step2:${item.step2}`)
                    this.newList.push({});
                    this.newList.pop();
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
        reLoad(){
            var that = this;
            if(!window.electron){
                that.isLoad = true;
                return;
            }
            this.registerListen(this,"setConfig",(cfg)=>{
                try{
                    // console.info("loadCfg:" ,cfg)
                    Object.assign(that.cfg, cfg);
                    that.isLoad = true;
                }catch(ex){
                    that.$message.error("配置加载异常:"+ex.message)
                }
            });
            electron.ipcRenderer.send("getConfig","")

        },
    }
    
}
</script>
